/*
 * ShopAllGoodsFragment.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.shopdetail.allgoods;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseEventFragment;
import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.recyclertype.DoubleGoods;
import com.yidiandao.business.ui.widget.dialog.LoadingDialog;
import com.yidiandao.business.ui.widget.recyclerview.LoadMoreRecyclerContainer;
import com.yidiandao.business.ui.widget.recyclerview.MultiTypeLoadMoreAdapter;
import com.yidiandao.business.ui.widget.recyclerview.provider.GoodsGridProvider;
import com.yidiandao.business.ui.widget.recyclerview.provider.GoodsListProvider;
import com.yidiandao.business.ui.widget.view.SortMenu;
import com.yidiandao.business.utils.AnimatorUtils;
import com.yidiandao.business.utils.SysUtils;
import com.yidiandao.business.utils.UIUtils;
import com.yidiandao.business.utils.UmengUtils;

import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;

/** 店铺详情-全部商品-View */
public class ShopAllGoodsFragment extends BaseEventFragment
        implements ShopAllGoodsContract.View, SortMenu.OnSortMenuClickListener {

    private static final String TAG = ShopAllGoodsFragment.class.getSimpleName();

    private ShopAllGoodsContract.Presenter mPresenter;

    private SortMenu mSortMenu;

    private RecyclerView mRecyclerView;

    private LoadMoreRecyclerContainer mLoadMoreContainer;

    private MultiTypeLoadMoreAdapter mGridAdapter;

    private MultiTypeLoadMoreAdapter mListAdapter;

    private LoadingDialog mLoadingDialog;

    private ImageView mIvToTop;

    private boolean isVisibility;

    private boolean isOpening, isHiding;

    public static ShopAllGoodsFragment getInstance(String supplierId) {
        ShopAllGoodsFragment fragment = new ShopAllGoodsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Intents.EXTRA_SUPPLIER_ID, supplierId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initViews() {
        mSortMenu = (SortMenu) mRootView.findViewById(R.id.sort_menu);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);
        mLoadMoreContainer =
                (LoadMoreRecyclerContainer) mRootView.findViewById(R.id.load_more_container);
        mIvToTop = (ImageView) mRootView.findViewById(R.id.iv_to_top);

        mSortMenu.setOnSortMenuClickListener(this);

        mPresenter.bundleSupplierId(getArguments());
        mPresenter.loadShopNewestGoods(true);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        mGridAdapter = new MultiTypeLoadMoreAdapter();
        mListAdapter = new MultiTypeLoadMoreAdapter();

        mGridAdapter.register(DoubleGoods.class, new GoodsGridProvider(getActivity()));
        mListAdapter.register(Goods.class, new GoodsListProvider(getActivity()));

        mLoadMoreContainer.setRecyclerViewAdapter(mGridAdapter);

        initListeners();
    }

    private void initListeners() {

        mLoadMoreContainer.useDefaultFooter();
        mLoadMoreContainer.setLoadMoreHandler(
                new LoadMoreHandler() {
                    @Override
                    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                        // 加载更多
                        switch (mSortMenu.getCurrentMenu()) {
                            case SortMenu.MENU_NEWEST:
                                mPresenter.loadShopNewestGoods(false);
                                break;
                            case SortMenu.MENU_PRICE:
                                mPresenter.loadShopPriceGoods(false, mSortMenu.isSortUp());
                                break;
                            case SortMenu.MENU_SALES:
                                mPresenter.loadShopSalesGoods(false);
                                break;
                        }
                    }
                });

        mLoadMoreContainer.setOnScrollListener(
                new RecyclerView.OnScrollListener() {
                    private int totalDy;

                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        totalDy -= dy;
                        if (dy > 0) {
                            // 动画隐藏
                            hideAnimator(mIvToTop);
                        } else {
                            if (Math.abs(totalDy) > SysUtils.getScreenHeight(
                                    UIUtils.getContext())) {
                                // 超过屏幕高度，开始显示
                                // 动画显示
                                showAnimator(mIvToTop);
                            } else {
                                // 隐藏
                                // 动画隐藏
                                hideAnimator(mIvToTop);
                            }
                        }
                    }
                });

        mIvToTop.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 返回顶部
                        mRecyclerView.smoothScrollToPosition(0);
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shop_all_goods;
    }

    /** 显示动画 */
    private void showAnimator(View view) {
        AnimatorUtils.showToTopAnimator(
                view,
                isVisibility,
                isOpening,
                new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        isOpening = true;
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        isVisibility = true;
                        isOpening = false;
                    }
                });
    }

    /** 隐藏动画 */
    private void hideAnimator(final View view) {
        AnimatorUtils.hideToTopAnimator(
                view,
                isVisibility,
                isHiding,
                new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        isHiding = true;
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        view.setVisibility(View.GONE);
                        isVisibility = false;
                        isHiding = false;
                    }
                });
    }

    @Override
    public void setPresenter(ShopAllGoodsContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return TAG;
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    public void onSortMenuClick(int menu, boolean isSortUp) {
        switch (menu) {
            case SortMenu.MENU_NEWEST:
                mPresenter.loadShopNewestGoods(true);
                break;
            case SortMenu.MENU_PRICE:
                UmengUtils.onEvent(mActivity, R.string.umeng_shop_detail_price);
                mPresenter.loadShopPriceGoods(true, isSortUp);
                break;
            case SortMenu.MENU_SALES:
                UmengUtils.onEvent(mActivity, R.string.umeng_shop_detail_sales);
                mPresenter.loadShopSalesGoods(true);
                break;
        }
    }

    @Override
    public void notifyDataChanged() {
        mPresenter.loadShopNewestGoods(true);
    }

    @Override
    public void onChangeListStyle(boolean isGridStyle) {
        changeAdapterStyle(!isGridStyle);
        mPresenter.changeStyle(isGridStyle);
    }

    @Override
    public void changeAdapterStyle(boolean isGridStyle) {
        // 当前是否是网格状布局
        mLoadMoreContainer.setRecyclerViewAdapter(isGridStyle ? mGridAdapter : mListAdapter);
        mLoadMoreContainer.useDefaultFooter();
    }

    @Override
    public void setGoodsGrid(List<DoubleGoods> goodsGridList) {
        mGridAdapter.setItems(goodsGridList);
        mGridAdapter.notifyDataSetChanged();
    }

    @Override
    public void setGoodsList(List<Goods> goodsList) {
        mListAdapter.setItems(goodsList);
        mListAdapter.notifyDataSetChanged();
    }

    @Override
    public void showLoadingDialog() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(mActivity);
            mLoadingDialog.setCanceledOnTouchOutside(false);
        }
        mLoadingDialog.show();
    }

    @Override
    public void hideLoadingDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void loadMoreFinish(boolean emptyResult, boolean hasMore) {
        mLoadMoreContainer.loadMoreFinish(emptyResult, hasMore);
    }

    @Override
    public void loadMoreError() {
        mLoadMoreContainer.loadMoreError(0, UIUtils.getString(R.string.load_more_failed));
    }
}
